package com.tongda.menu.activity;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.tongda.core.common.Constants;
import com.tongda.core.common.KernalApplication;
import com.tongda.core.service.RestaurantService;
import com.tongda.core.utils.SharedPreferencesUtils;
import com.tongda.core.vo.RestNews;
import com.tongda.menu.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class RstStateActivity extends BaseActivity{
	
	private List<RestNews> restNews;
	private ListView rstNewsLv;
	private KernalApplication application;
	
	Handler newsHandler  = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			restNews = (List<RestNews>) msg.getData().getSerializable("news");
			if(restNews.size()==0){
				rstNoRly.setVisibility(View.VISIBLE);
				rstNewsLv.setVisibility(View.GONE);
				rstStateOrder.setText(Html.fromHtml("暂无餐厅动态哦！"+"<font color='#a9d300'>"+"去点菜>>"+"</font>"));
				rstStateOrder.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						RstStateActivity.this.finish();
						RstStateActivity.this.overridePendingTransition(R.anim.in_from_bottom, R.anim.out_to_top);
					}
				});
				
			}else{
				rstNoRly.setVisibility(View.GONE);
				rstNewsLv.setVisibility(View.VISIBLE);
			}
			RestNewsAdapter newsAdapter = new RestNewsAdapter(restNews, RstStateActivity.this);
			rstNewsLv.setAdapter(newsAdapter);
			
			rstNewsLv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					RestNews news = restNews.get(position);
					final AlertDialog dialog = new AlertDialog.Builder(RstStateActivity.this).create();
					View dialogView = View.inflate(RstStateActivity.this, R.layout.dialog_restnews, null);
					ImageView cancelImv =  (ImageView)dialogView.findViewById(R.id.dialog_news_cancel);
					TextView titleTv = (TextView)dialogView.findViewById(R.id.dialog_news_title);
					TextView manTv = (TextView)dialogView.findViewById(R.id.dialog_news_creatMan);
					TextView timeTv = (TextView)dialogView.findViewById(R.id.dialog_news_creatTime);
					TextView contentTv = (TextView)dialogView.findViewById(R.id.dialog_news_content);
					
					titleTv.setText(news.getTitle());
					manTv.setText("创建人："+news.getCreatMan());
					timeTv.setText("创建时间："+news.getCreatTimeStr());
					
					String regEx="<[^>]*>"; //去掉所有的html标签
					Pattern pat=Pattern.compile(regEx);  
					Matcher mat=pat.matcher(news.getContent());  
					String content=mat.replaceAll("");   
					
					contentTv.setText(content);
					
					cancelImv.setOnClickListener(new OnClickListener() { 
						
						@Override
						public void onClick(View v) {
							dialog.dismiss();
						}
					});
					
					dialog.setView(dialogView, 0, 0, 0, 0);
					dialog.show();
				}
			});
		}
	};
	private TextView rstStateName;
	private RelativeLayout rstNoRly;
	private TextView rstStateOrder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rststate);
		application = KernalApplication.getInstance();
		rstNewsLv = (ListView)findViewById(R.id.rst_newsLv);
		rstStateName = (TextView)findViewById(R.id.rststate_name);
		rstNoRly = (RelativeLayout)findViewById(R.id.rststate_noRly);
		rstStateOrder = (TextView)findViewById(R.id.rststate_go_order);
		String rstName = (String) SharedPreferencesUtils.getParam(RstStateActivity.this, Constants.MENU_RES_NAME, "");
		if("".equals(rstName)){
			rstStateName.setText(rstName);
		}else{
			rstStateName.setText("餐厅动态");
		}
		Thread getRestNewsThread  = new Thread(new getRestNews());
		getRestNewsThread.start();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			RstStateActivity.this.finish();
			RstStateActivity.this.overridePendingTransition(R.anim.in_from_bottom, R.anim.out_to_top);
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public class getRestNews implements Runnable{

		@Override
		public void run() {
			while(true){
				try {
					restNews = RestaurantService.getRestNews();
					Message msg = newsHandler.obtainMessage();
					Bundle data = new Bundle();
					data.putSerializable("news", (Serializable) restNews);
					msg.setData(data );
					newsHandler.sendMessage(msg);
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public class RestNewsAdapter extends BaseAdapter{

		List<RestNews> datas;
		Context context;
		
		public RestNewsAdapter(List<RestNews> datas,Context context){
			this.datas = datas;
			this.context = context;
		}
		
		@Override
		public int getCount() {
			return datas.size();
		}

		@Override
		public Object getItem(int position) {
			return datas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if(convertView==null){
				holder = new Holder();
				convertView = View.inflate(context, R.layout.item_restnews, null);
				holder.news_contentTv = (TextView) convertView.findViewById(R.id.item_news_content);
				holder.news_creatTimeTv = (TextView)convertView.findViewById(R.id.item_news_creatTime);
				convertView.setTag(holder);
			}else{
				holder = (Holder)convertView.getTag();
			}
			RestNews restNews= datas.get(position);
			
			//处理字符串,将里面的一些标签类的东西去掉
			String contenTemp = restNews.getTitle();
			String regEx="<[^>]*>"; //去掉所有的html标签
			Pattern pat=Pattern.compile(regEx); 
			Matcher mat=pat.matcher(contenTemp);  
			String content=mat.replaceAll("");   
			holder.news_contentTv.setText(content);
			holder.news_contentTv.setSelected(true);
			holder.news_creatTimeTv.setText(restNews.getCreatTimeStr());
			return convertView;
		}
		
		class Holder {
			TextView news_contentTv;
			TextView news_creatTimeTv;
		}
		
	}
}
